﻿/************************************************************************
* Copyright (c) 2018 All Rights Reserved.
* 命名空间：DavidHelper
* 文件名：Utils
* 创建人：David Rodríguez
* 创建时间：2018/8/4 星期六 11:10:15
* 描述：
*************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DavidHelper
{
    public static class Utils
    {
        public static bool IsNumberic(string message, out int result)
        {
            var rex =
                new System.Text.RegularExpressions.Regex(@"^\d+$");
            result = -1;
            if (rex.IsMatch(message))
            {
                result = int.Parse(message);
                return true;
            }
            else
                return false;
        }

        public static string ToJson(this object model)
        {
            return JsonHelper.SerializeObeject(model);
        }


        /// <summary>
        /// Base64编码
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string ToBase64String(this string value)
        {
            if (string.IsNullOrEmpty(value))
                return "";
            var bytes = Encoding.UTF8.GetBytes(value);
            return Convert.ToBase64String(bytes);
        }

        /// <summary>
        /// Base64解码
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string UnBase64String(this string value)
        {
            if (string.IsNullOrEmpty(value))
                return "";
            var bytes = Convert.FromBase64String(value);
            return Encoding.UTF8.GetString(bytes);
        }

    }
}